package com.lans.SortAlgorithm;

import java.util.Arrays;

/*
 * @author: lans
 * @date: 2025/1/20
 * @name: 刘宇
 */
public class InsertSort1 {
    public static void main(String[] args) {
        int[] arrays = new int[]{2, 6, 4, 1, 3, 7, 5};
        insertSort(arrays);
        System.out.println(Arrays.toString(arrays));
    }
    public static void insertSort(int[] arr) {
        for(int i = 1;i<arr.length;i++) {
            int temp = arr[i];
            int j = i-1;//比较的后第一位元素
            while(j>=0&&arr[j]>temp) {
                arr[j+1] = arr[j];
                j = j-1;//索引后移
            }
            arr[j+1]=temp;
        }
    }
}
